import Request from 'luch-request'//npm下载引入luch-request
import qs from 'qs'
const http = new Request({
	// baseURL: "http://localhost:9999/", //设置请求的base url
	timeout: 120000, //超时时长5分钟,
	header: {
		'Content-Type': 'application/x-www-form-urlencoded',
		'TENANT-ID': 1
	}
})

//请求拦截器
http.interceptors.request.use((config) => { // 可使用async await 做异步操作
	const token = uni.getStorageSync('token');
	if (token) {
		config.header = {
			...config.header,
			'Authorization': 'Bearer ' + token
		}
	}else{
		config.header = {
			...config.header,
			'Authorization': 'Basic cGlnOnBpZw=='
		}
	}

	if (config.method === 'POST') {
		config.data = qs.stringify(config.data);
	}
	return config
}, error => {
	return Promise.resolve(error)
})

// 响应拦截器
http.interceptors.response.use((response) => {
	console.log(response)
	return response
}, (error) => {
	//未登录时清空缓存跳转
	if (error.statusCode == 401) {
		uni.clearStorageSync();
		uni.switchTab({
			url: "/pages/auth/passlogin"
		})
	}
	return Promise.resolve(error)
})
export default http;

